//
//  WSAppConfig.m
//  CharonTVProject
//
//  Created by karong on 2023/7/6.
//

#import "WSAppConfig.h"
#import "NSBundle+Language.h"

@implementation WSAppConfig
static WSAppConfig *instanceAppConfig = nil;

+ (NSArray *)mj_ignoredPropertyNames {
    return @[];
}

+ (instancetype)defaultSession {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instanceAppConfig = [[self alloc] init];
    });
    return instanceAppConfig;
}

- (instancetype)init {
    if([appUserDefaults objectForKey:@"WSAppConfig"]) {
        self = [[WSAppConfig alloc] mj_setKeyValues:[appUserDefaults objectForKey:@"WSAppConfig"]];
    }
    else {
        self = [super init];
        self.isAutoPay = YES;
        NSString *lanStr = [NSBundle currentLanguage];
//        if ([lanStr containsString:@"en"]){
//            self.laType = 2;
//        }else if ([lanStr containsString:@"Hans"]){
//            self.laType = 1;
//        }else{
//            self.laType = 3;
//        }
        self.laType = 1;
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(save) name:UIApplicationWillTerminateNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(save) name:UIApplicationDidEnterBackgroundNotification object:nil];
    return self;
}

- (void)save {
    @try {
        [appUserDefaults setObject:instanceAppConfig.mj_keyValues forKey:@"WSAppConfig"];
    } @catch (NSException *exception) {
        [appUserDefaults removeObjectForKey:@"WSAppConfig"];
    } @finally {
        [appUserDefaults synchronize];
    }
}
@end

@implementation WSStartConfig

@end
